﻿app.filter('startFrom', function () {
    return function (input, start) {
        if (input) {
            start = +start; //parse to int
            return input.slice(start);
        }
        return [];
    }
});
app.controller('ZoneController', ['$scope', 'GlobalService', 'ApplicationService', 'alertboxservice', function ($scope, GlobalService, ApplicationService, alertboxservice) {

    BindGrid();

    $scope.toggle = function (modelstate, id) {
        $scope.modelstate = modelstate;
        $scope.id = id;
        switch (modelstate) {
            case 'add':
                $scope.title = "Add Organization Zones";
                $scope.OrganizationPartition = null;
                break;
            case 'edit':
                $scope.title = "Edit Organization Zones";
                url = "OrganizationZones/GetOrganizationZone";
                ApplicationService.Getdata(url, id).success(function (data) {
                    $scope.OrganizationPartition = data;
                });
                break;
        }
        $("#Zone").modal({ backdrop: 'static' });
    }

    function BindGrid() {
        url = "OrganizationZones/GetOrganizationZones";
        ApplicationService.Getdatas(url).success(function (data) {
            $scope.list = data;
            $scope.currentPage = 1;
            $scope.entryLimit = 10;
            $scope.filteredItems = $scope.list.length;
            $scope.totalItems = $scope.list.length;
        });
    }

    $scope.setPage = function (pageNo) {
        $scope.currentPage = pageNo;
    };

    $scope.sort_by = function (predicate) {
        $scope.predicate = predicate;
        $scope.reverse = !$scope.reverse;
    };

    $scope.delete = function (data, dataid) {
        swal({
            title: "Are you sure?",
            text: "Your will not be able to recover " + data + " Details!",
            type: "warning",
            showCancelButton: true,
            cancelButtonClass: 'btn-outline btn-black mr-10',
            cancelButtonText: 'Cancel',
            confirmButtonClass: "btn-danger",
            confirmButtonText: "Delete",
            closeOnConfirm: false
        }, function (status) {
            if (status) {
                url = "OrganizationZones/DeleteOrganizationPartion";
                ApplicationService.DeleteData(url, dataid, data).success(function (data) {
                    if (data == "DSUS") {
                        swal("Deleted!", "Details deleted successfully", "success");
                    }
                    else {
                        swal("failed", "Unable to delete this details", "error");
                    }
                    BindGrid();
                });
            }
        });


    }


    


    $scope.save = function (modelstate, id) {
        $scope.buttonstate = true;
        var url;
        switch (modelstate) {
            case 'add':
                url = "OrganizationZones/AddOrganizationZone";
                break;
            case 'edit':
                url = "OrganizationZones/UpdateOrganizationPartion";
                break;
        }
        ApplicationService.Save($scope.OrganizationPartition, url).success(function (result) {
            alertboxservice.ShowMsg(result, "Church ");
            $scope.buttonstate = false;
            BindGrid();
        });
    }

    $scope.ZoneName = "";
    $scope.$watch('OrganizationPartition.OrganizationId', function (id) {
        var min = [];
        $scope.OrganizationName = $("#dpminitrytype option:selected").html();
        for (var i = 0; i < id - 1; i++) {
            var mtpe = {
                Name: "",
                id: ""
            }
            mtpe.id = $scope.minitrialtype[i].Departmentsid;
            mtpe.Name = $scope.minitrialtype[i].DepartmentName;
            min.push(mtpe);
        }
        $scope.mintry = min;
    });

    
    GlobalService.GetMinitrialType().success(function (data) {
        $scope.minitrialtype = data;
    });

}]);

